package com.int_tech.newTimeTask;

import com.int_tech.MessageCodeAll.dao.CodeA0Dao;
import com.int_tech.MessageCodeAll.entity.CodeA0;
import com.int_tech.MessageCodeAll.entity.CodeA2;
import com.int_tech.control.dao.SocketLogSendDao;
import com.int_tech.control.entity.SocketLogSend;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.List;

/**
 * Created by liubochao on 2018/6/22.
 */
@Component
public class CheckA0TimeTask {

    @Autowired
    private SocketLogSendDao socketLogSendDao;

    @Autowired
    private CodeA0Dao codeA0Dao;

    @Scheduled(cron="0/10 * * * * ?")
    public void checkA0() throws ParseException {

        List<SocketLogSend> socketLogSendList = socketLogSendDao.findByCodeAndCheckStatus("A0",0);

        for(SocketLogSend socketLogSend : socketLogSendList)
        {
            try
            {
                socketLogSend.setCheckStatus(1);
                socketLogSendDao.save(socketLogSend);

                createA0Message(socketLogSend);
            }
            catch (Exception e){

                e.printStackTrace();

                socketLogSend.setCheckStatus(2);

                socketLogSendDao.save(socketLogSend);
            }
        }
    }


    //计算装载率
    @Scheduled(cron="0/10 * * * * ?")
    public void checkRat() throws ParseException {


        List<CodeA0> codeA0List = codeA0Dao.findByStatus(0);

        for(CodeA0 codeA0:codeA0List)
        {

            try
            {
                codeA0.setStatus(1);

                codeA0Dao.save(codeA0);

                String logData = codeA0.getDataString();

                String[] logs = logData.split(",");


                //长宽高
                float x = Float.parseFloat(logs[10]);
                float y = Float.parseFloat(logs[11]);
                float z = Float.parseFloat(logs[12]);

              /*  x = x/100;
                y = y/100;
                z = z/100;*/



                //行列
                int hang = Integer.parseInt(logs[13]);
                int lie = Integer.parseInt(logs[14]);

                //行列间距
                int hangjianju = Integer.parseInt(logs[15]);
                int liejianju = Integer.parseInt(logs[16]);

                //前后
                int qian = Integer.parseInt(logs[17]);
                int hou = Integer.parseInt(logs[18]);

                //左右
                int zuo = Integer.parseInt(logs[19]);
                int you = Integer.parseInt(logs[20]);


                //特殊点位置
                //右上角点,一定是第一点
                int pointysj = 21;
                //左上角点，为第一点+行数-1
                int pointzsj = 20+hang;
                //左下角点，为最后一个点
                int pointzxj = logs.length-1;
                //右下角点，为最后一个点-行数+1
                int pointyxj = logs.length-hang;
                //左右第二列
                int pointz2l = 0;
                int pointy2l = 0;
                //左右第三列
                int pointz3l = 0;
                int pointy3l = 0;



                //所有特殊点位,根据列判断
                if(lie == 2)
                {
                    //如果是两列，没有最中间点，全是周围点
                }
                else if(lie == 3)
                {
                    //中间有一列
                    pointz3l = pointyxj-1;
                    pointy3l = pointzsj+1;
                }
                else if(lie == 4)
                {
                    //中间有两列
                }

                double rateTotal = 0;
                double total = x*y*z;
                double rate = 0;
                String rateString = "";
                int totalArea = 0;
                int totalCount = 0;

                int a1 = 0;
                int a2 = 0;
                int a3 = 0;
                int a4 = 0;

                int isFull = 0;
                for (int m = 21;m<21+hang*lie;m++)
                {
                    int c = Integer.parseInt(logs[m]);
                    if((c > 100)&&(c != 9999))
                    {
                        isFull = 1;
                    }
                    if(isFull == 1)
                    {
                        break;
                    }
                }


                //如果isFull为1，证明有货
                if(isFull == 1)
                {
                    String buchang = "";

                    for(int i=21;i<21+hang*lie;i++)
                    {
                        if("9999".equals(logs[i]))
                        {
                            //9999做补偿
                            logs[i] = buchang;
                        }
                        else
                        {
                            buchang = logs[i];
                        }
                        try
                        {
                            totalCount++;
                            //传感器面积
                            int area = 0;

                            //四个角上的点
                            if(i == pointysj||i == pointzsj||i == pointzxj||i == pointyxj)
                            {
                                area = (hangjianju/2+zuo)*(liejianju/2+qian);
                                a1 += area;
                            }
                            else if(i == pointz3l||i == pointy3l)
                            {
                                //中间两头的点
                                area = hangjianju*(hangjianju/2+qian);
                                a2 += area;
                            }
                            else if(i > 21&&i < (20+hang))
                            {
                                //第一行中间
                                area = hangjianju*(hangjianju/2+zuo);
                                a3 += area;
                            }
                            else if(i > logs.length-hang&&i < logs.length-1)
                            {
                                //最后一行中间
                                area = hangjianju*(hangjianju/2+zuo);
                                a3 += area;
                            }
                            else
                            {
                                //最中间
                                area = hangjianju * liejianju;
                                a4 += area;
                            }
                            int h = Integer.parseInt(logs[i]);
                            rateTotal += (area*h)/10;

                            if(StringUtils.isEmpty(rateString))
                            {
                                rateString += logs[i];
                            }
                            else
                            {
                                rateString += ","+logs[i];
                            }

                            totalArea += area;
                        }
                        catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                    }

                    double tr = rateTotal * 100;
                    rate = tr/total;

                    DecimalFormat df   = new DecimalFormat("######0.00");
                    codeA0.setRate(String.valueOf(df.format(rate)));

                    codeA0Dao.save(codeA0);
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();

                codeA0.setStatus(-2);

                codeA0Dao.save(codeA0);
            }
        }
    }


    /**
     * 将装载率为空的设置为0
     * @throws ParseException
     */
    @Scheduled(cron="0/10 * * * * ?")
    public void setRateIsNull() throws ParseException {


        List<CodeA0> codeA0List = codeA0Dao.findAll();

        for (CodeA0 codeA0 :codeA0List)
        {
            if(StringUtils.isEmpty(codeA0.getRate()))
            {
                codeA0.setRate("0");
                codeA0Dao.save(codeA0);
            }
        }

    }






        public void createA0Message(SocketLogSend socketLog1) {

        String logData = socketLog1.getDataString();

        String[] logs = logData.split(",");

        String carNumber = logs[2];

        CodeA0 codeA0 = new CodeA0();

        codeA0.setCarNumber(carNumber);
        codeA0.setCreatedTime(socketLog1.getCreateTime());

        codeA0.setDataString(socketLog1.getDataString());

        //长宽高
        float x = Float.parseFloat(logs[10]);
        float y = Float.parseFloat(logs[11]);
        float z = Float.parseFloat(logs[12]);

        //修改车厢长宽高
        float xx = x/100;
        float yy = y/100;
        float zz = z/100;

        //经纬度
        String lng = logs[7];
        String lat = logs[8];

        codeA0.setLng(lng);
        codeA0.setLat(lat);

        codeA0.setLength(String.valueOf(xx));
        codeA0.setWidth(String.valueOf(yy));
        codeA0.setHeight(String.valueOf(zz));

        //行列
        int hang = Integer.parseInt(logs[13]);
        int lie = Integer.parseInt(logs[14]);

        codeA0.setHang(String.valueOf(hang));
        codeA0.setLie(String.valueOf(lie));

        String rateString = "";

        int isFull = 0;
        for (int m = 21;m<21+hang*lie;m++)
        {
            int c = Integer.parseInt(logs[m]);
            if((c > 100)&&(c != 9999))
            {
                isFull = 1;
            }
            if(isFull == 1)
            {
                break;
            }
        }

        if(isFull == 0)
        {
            //空订单
            codeA0.setStatus(-1);
        }


        String buchang = "";

        for(int i=21;i<21+hang*lie;i++)
        {
            if("9999".equals(logs[i]))
            {
                //9999做补偿
                logs[i] = buchang;
            }
            else
            {
                buchang = logs[i];
            }
            try
            {

                if(StringUtils.isEmpty(rateString))
                {
                    rateString += logs[i];
                }
                else
                {
                    rateString += ","+logs[i];
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }

        codeA0.setRateDateString(rateString);

        codeA0Dao.save(codeA0);
    }
}
